//
//  BSKTextView.m
//  MicroMemory
//
//  Created by rimi on 16/7/12.
//  Copyright © 2016年 BlueSky. All rights reserved.
//

#import "BSKTextView.h"
#import "BSKconsts.h"


@interface BSKTextView ()
@property(nonatomic,strong)NSAttributedString * cutStr ;

@end

@implementation BSKTextView

- (instancetype)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self) {
		self.font = [UIFont systemFontOfSize:18];
		_textBgColor = [UIColor redColor];
		//添加长按弹出的菜单选项
		UIMenuItem * BgColoritem = [[UIMenuItem alloc]initWithTitle:@"高亮" action:@selector(textBgColorAction:)];
		UIMenuItem * unBgColoritem = [[UIMenuItem alloc] initWithTitle:@"取消高亮" action:@selector(removeBgColorAction:)];
		UIMenuController * controller = [UIMenuController sharedMenuController];
		controller.menuItems = @[BgColoritem,unBgColoritem];
	}
	return self;
}
//在适当的条件下显示不同的弹出菜单按钮
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
	if (self.selectedRange.length>0) {
		if (self.isEditable) {
			if (action==@selector(cut:)||action==@selector(textBgColorAction:)||action== @selector(removeBgColorAction:)) {
				return YES;
			}
		}
		if (action==@selector(copy:)) {
			return YES;
		}
		
	}
	if (self.selectedRange.length!=self.attributedText.length&&action==@selector(selectAll:)) {
		return YES;
	}
	if (action==@selector(paste:)&&self.isEditable) {
		return YES;
	}
	return NO;
}

-(void)textBgColorAction:(UIMenuItem *)sender{
	NSRange range =NSMakeRange(self.selectedRange.location+self.selectedRange.length, 0);
	NSMutableAttributedString * mutableAttrStr = [self.attributedText mutableCopy];
	
	NSAttributedString * str =[self.attributedText attributedSubstringFromRange:self.selectedRange];
	NSMutableAttributedString * mstr = [str mutableCopy];
	
	[mstr addAttribute:NSBackgroundColorAttributeName value:_textBgColor range:NSMakeRange(0, mstr.length)];
	if([mstr isEqualToAttributedString:str] == 1){
		[mutableAttrStr removeAttribute:NSBackgroundColorAttributeName range:self.selectedRange];
	}
	else {
		[mutableAttrStr addAttribute:NSBackgroundColorAttributeName value:_textBgColor range:self.selectedRange];
	}
	self.attributedText = mutableAttrStr;
	self.selectedRange = range;
	[self scrollRangeToVisible:range];
}


-(void)textColorAction:(UIColor *)textColor{
	NSRange range =NSMakeRange(self.selectedRange.location+self.selectedRange.length, 0);
	NSMutableAttributedString * mutableAttrStr = [self.attributedText mutableCopy];
	
	NSAttributedString * str =[self.attributedText attributedSubstringFromRange:self.selectedRange];
	NSMutableAttributedString * mstr = [str mutableCopy];
	
	[mstr addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(0, mstr.length)];
	if([mstr isEqualToAttributedString:str]){
		[mutableAttrStr removeAttribute:NSForegroundColorAttributeName range:self.selectedRange];
	}
	else {
		[mutableAttrStr addAttribute:NSForegroundColorAttributeName value:textColor range:self.selectedRange];
	}
	self.attributedText = mutableAttrStr;
	self.selectedRange = range;
	[self scrollRangeToVisible:range];
}

-(void)removeTextColor:(NSRange)range{
	self.selectedRange = range;
	NSMutableAttributedString * mutableAttrStr = [self.attributedText mutableCopy];
	[mutableAttrStr removeAttribute:NSForegroundColorAttributeName range:range];
	self.attributedText = mutableAttrStr;
	self.selectedRange = NSMakeRange(mutableAttrStr.length,0);
	[self scrollRangeToVisible:range];
}

-(void)removeBgColorAction:(UIMenuItem *)sender{
	NSRange range =NSMakeRange(self.selectedRange.location+self.selectedRange.length, 0);
	NSMutableAttributedString * mutableAttrStr = [self.attributedText mutableCopy];
	[mutableAttrStr removeAttribute:NSBackgroundColorAttributeName range:self.selectedRange];
	self.attributedText = mutableAttrStr;
	self.selectedRange = range;
	[self scrollRangeToVisible:range];
}


-(void)removeBgColor:(NSRange)range{
	self.selectedRange = range;
	NSMutableAttributedString * mutableAttrStr = [self.attributedText mutableCopy];
	[mutableAttrStr removeAttribute:NSBackgroundColorAttributeName range:range];
	self.attributedText = mutableAttrStr;
	self.selectedRange = NSMakeRange(mutableAttrStr.length,0);
	[self scrollRangeToVisible:range];
}

-(void)removeUnderliner:(NSRange)range{
	self.selectedRange = range;
	NSMutableAttributedString * mutableAttrStr = [self.attributedText mutableCopy];
	[mutableAttrStr removeAttribute:NSUnderlineStyleAttributeName range:range];
	self.attributedText = mutableAttrStr;
	self.selectedRange = NSMakeRange(mutableAttrStr.length,0);
	[self scrollRangeToVisible:range];
}

-(void)underlineAction{
	NSRange range =NSMakeRange(self.selectedRange.location+self.selectedRange.length, 0);
	NSMutableAttributedString * mutableAttrStr = [self.attributedText mutableCopy];
	
	NSAttributedString * str =[self.attributedText attributedSubstringFromRange:self.selectedRange];
	NSMutableAttributedString * mstr = [str mutableCopy];
	
	
	[mstr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlinePatternDot] range:NSMakeRange(0, mstr.length)];
	[mstr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range:NSMakeRange(0, mstr.length)];
	
	if([mstr isEqualToAttributedString:str] == 1){
		[mutableAttrStr removeAttribute:NSUnderlineStyleAttributeName range:self.selectedRange];
		[mutableAttrStr removeAttribute:NSUnderlineStyleAttributeName range:self.selectedRange];
		
	}
	else {
		[mutableAttrStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlinePatternDot] range:self.selectedRange];
		[mutableAttrStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range:self.selectedRange];
	}
	self.attributedText = mutableAttrStr;
	self.selectedRange = range;
	[self scrollRangeToVisible:range];
	
}

-(void)removeAllAttributeInRange:(NSRange)range{
	[self removeBgColor:range];
	[self removeTextColor:range];
	[self removeUnderliner:range];
}

//保证复制粘贴能够保留原来的文字属性，所以重写剪切粘贴
-(void)cut:(id)sender{
	_cutStr = [self.attributedText attributedSubstringFromRange:self.selectedRange];
}

-(void)copy:(id)sender{
	_cutStr = [self.attributedText attributedSubstringFromRange:self.selectedRange];
}

-(void)paste:(id)sender{
	if(_cutStr){
		NSRange range =self.selectedRange;
		NSMutableAttributedString * mutableAttrStr = [self.attributedText mutableCopy];
		[mutableAttrStr replaceCharactersInRange:range withAttributedString:_cutStr];
		self.attributedText = mutableAttrStr;
		[self scrollRangeToVisible:range];
	}else{
		[super paste:sender];
	}
}
-(void)setTextContainerInset:(UIEdgeInsets)textContainerInset{
	[super setTextContainerInset:textContainerInset];
	[self setNeedsDisplay];
}

@end
